package com.qiezi.project.contract;

import com.alibaba.fastjson.TypeReference;
import com.qiezi.bean.DataBean;
import com.qiezi.bean.ResponseBean;
import com.qiezi.bean.VideoBean;
import com.qiezi.mvp.BaseMvpView;
import com.qiezi.mvp.BasePresenter;
import com.qiezi.util.Request;
import com.zhy.http.okhttp.request.RequestCall;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;



public class CategoryContentContract {
    public static class Presenter extends BasePresenter<View>{


        private Map<String,String> params;

        public void loadMore(Integer id,int page){
            loadData(id,page);
        }

        private RequestCall cell;

        public void canceledCell(){
            if (cell != null)cell.cancel();
            mView.complete();
        }

        public void loadData(Integer id,final int page){
            if (params == null){
                params = new HashMap<>();
            }
            params.put("category_id",id.toString());
            params.put("page",String.valueOf(page));
            if (cell != null) cell.cancel();
            cell = Request.post(mView.getApi(), params, new Request.JsonCallback<DataBean<VideoBean>>(new TypeReference<DataBean<VideoBean>>(){}.getType()) {
                @Override
                public void onFailure(String msg) {
                    if (mView == null)return;
                    mView.showError(msg);
                }

                @Override
                public void onSueccess(ResponseBean<DataBean<VideoBean>> data) {
                    if (mView == null)return;
                    if (page == 1){
                        mView.onRefresh(data.getData().getList());
                    }else {
                        mView.onLoadMore(data.getData().getList());
                    }
                }

            });
        }
    }

    public interface View extends BaseMvpView {
        void onRefresh(ArrayList<VideoBean> data);
        void onLoadMore(ArrayList<VideoBean> data);
        Request.Api getApi();
    }
}